package com.jiyun.purse.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author zhaomingshuo
 * @Date: 2024/08/30
 */

@Slf4j
public class BankCards {


    public static void main(String[] args) {
        String host = "https://mtbank.market.alicloudapi.com";
        String path = "/data/verify_bankcard";
        String method = "POST";
//        自己账号的appcode
        String appcode = "622d686883504b1997c1b504c33fb4d5";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        //需要给X-Ca-Nonce的值生成随机字符串，每次请求不能相同
        headers.put("X-Ca-Nonce", UUID.randomUUID().toString());
//        headers.put("X-Ca-Nonce","6214686362041837");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("card_number","6221801000020899222");
//        bodys.put("ext", "ext");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            log.info("response:{}",response.toString());
            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity()));

            String jsonStr = EntityUtils.toString(response.getEntity());

            System.out.println(jsonStr);
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                ResultData resultData = objectMapper.readValue(jsonStr, ResultData.class);
                System.out.println("Request ID: " + resultData.getRequest_id());
                System.out.println("Bank: " + resultData.getResult().getBank());
                System.out.println("Bank Icon: " + resultData.getResult().getLogo());
                // 其他属性的访问方式类似
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

